package com.example.dameng.controller;

import com.example.dameng.api.CommonResult;
import com.example.dameng.entity.*;
import com.example.dameng.mapper.AdminMapper;
import com.example.dameng.service.AdminService;
import com.example.dameng.utils.MonthList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@ResponseBody
@RestController
public class AdminController {
    @Autowired
    AdminService adminService;

    //分局账号列表
    @RequestMapping(value = "/admin/subInfo")
    public CommonResult subInfo(){
        List<T_admininfo> sublist = adminService.getSubInfo();
        return CommonResult.success(sublist);
    }

    //分局账号增删改
    @RequestMapping(value = "/admin/addSub")
    public CommonResult addSub(@RequestBody T_admininfo admininfo){

        try {
            int n = adminService.addSubInfo(admininfo);
            if(n!=0){
                return CommonResult.success("");
            }
            else {
                return CommonResult.failed("增加失败");
            }
        }catch (Exception e){
            return CommonResult.failed("增加失败");
        }

    }
    @RequestMapping(value = "/admin/deleteSub")
    public CommonResult deleteSub(@RequestParam int adminCode){
        try{
            int n = adminService.deleteSubInfo(adminCode);
            if(n!=0){
                return CommonResult.success("");
            }
            else {
                return CommonResult.failed("删除失败");
            }
        }catch (Exception e){
            return CommonResult.failed("删除失败");
        }
    }
    @RequestMapping(value = "/admin/updateSub" )
    public CommonResult updateSub(@RequestBody T_admininfo admininfo){
        try {
            int n = adminService.updateSubInfo(admininfo);
            if(n!=0){
                return CommonResult.success("");
            }
            else {
                return CommonResult.failed("删除失败");
            }
        }catch (Exception e){
            return CommonResult.failed("删除失败");
        }

    }

    //区域列表
    @RequestMapping(value = "/admin/getArea")
    public CommonResult getArea(){
        List<T_area> array = adminService.GetArea();
        System.out.println(array);
        return CommonResult.success(array);
    }

    //区域用电详细信息列表
    @RequestMapping(value = "/admin/areaInfoList")
    public CommonResult areaInfoList(@RequestParam String date) throws ParseException {
        List<AreaList> list;
        if(date != ""){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String str=date;
            Date dt=sdf.parse(str);
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(dt);
            rightNow.add(Calendar.DAY_OF_YEAR,-1);//日期减1天
            Date dt1=rightNow.getTime();
            String yesterday = sdf.format(dt1);
            list = adminService.getAreaInfo2(yesterday,date);
        }else {
            list = adminService.getAreaInfo();
        }
        return CommonResult.success(list);
    }

    //取待审核信息
    @RequestMapping(value = "/admin/applyList")
    public CommonResult applyList(){
        List<T_supplystatus> list = adminService.getApplyList();
        return CommonResult.success(list);
    }

    //审核操作
    @RequestMapping(value = "/admin/checkApply")
    public CommonResult checkApply(@RequestBody T_supplystatus checkStatus){
        checkStatus.setApprovalTime(new Timestamp(System.currentTimeMillis()));
        int n = adminService.updateSupplyStatus(checkStatus);
        int m = adminService.updateSupplyLog(checkStatus);
        System.out.println("---审核测试---");
        System.out.println(n);
        System.out.println(m);
        return CommonResult.success("");
    }

    //月度报表
    @RequestMapping(value = "/admin/report")
    public CommonResult report(@RequestParam String startMonth,@RequestParam String endMonth,@RequestParam String areaName){
        System.out.println("---admin报表测试---");
        System.out.println(areaName);
        List<String> list = MonthList.getMonthBetweenDate(startMonth,endMonth);
        List<AdminReport> reportList = new ArrayList<>();
        System.out.println(list);
        for(String s:list){
            System.out.println(s);
            reportList.add(adminService.getMonthReport(s,areaName));
        }
        return CommonResult.success(reportList);
    }
}
